 /**
   * Copyright 2014 Viettel Telecom. All rights reserved.
   * VIETTEL PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
   */
 package com.viettel.core.dao.cms; 
import java.util.ArrayList;
import java.util.List;

import org.hibernate.type.StandardBasicTypes;
import org.hibernate.type.Type;
import org.springframework.beans.factory.annotation.Autowired;

import com.viettel.core.common.utils.StringUtil;
import com.viettel.core.entities.cms.ArticleCategory;
import com.viettel.core.entities.cms.CmsUser;
import com.viettel.core.entities.cms.GroupCategory;
import com.viettel.core.entities.cms.Media;
import com.viettel.core.entities.cms.PublicArticle;
import com.viettel.core.entities.cms.TechniqueCategory;
import com.viettel.core.entities.vo.PublicArticleSearchFilter;
import com.viettel.core.entities.vo.PublicArticleSearchVO;
import com.viettel.core.enumtype.ArticleStatus;
import com.viettel.core.exceptions.DataAccessException;
import com.viettel.core.repo.IRepository;
 /**
   *  Mo ta muc dich cua lop (interface)
   *  @author: TuanTM20
   *  @version:  
   *  @since: 27/02/2014 - 11:34:02
   */
 public class PublicArticleDAOImpl implements PublicArticleDAO { 
	 private IRepository repo; 
		
	 	public void setRepo(IRepository repo) {
			this.repo = repo;
		}
 
 	/**@author TuanTM20
  	 * 27/02/2014 - 11:34:02
  	 * For: 
  	 * @description: 
  	 */
 	@Override 
	public PublicArticle getPublicArticleById(Integer id) throws DataAccessException { 
		return repo.getEntityById(PublicArticle.class, id); 
	} 
 
 	/**@author TuanTM20
  	 * 27/02/2014 - 11:34:02
  	 * For: 
  	 * @description: 
  	 */
 	@Override 
	public PublicArticle createPublicArticle(PublicArticle publicArticle) 
			throws DataAccessException { 
		if (publicArticle == null) { 
			throw new IllegalArgumentException("publicarticle"); 
		} 
		repo.create(publicArticle); 
		return repo.getEntityById(PublicArticle.class, publicArticle.getId()); 
	} 
 
 	/**@author TuanTM20
  	 * 27/02/2014 - 11:34:02
  	 * For: 
  	 * @description: 
  	 */
 	@Override 
	public void updatePublicArticle(PublicArticle publicArticle) throws DataAccessException { 
		if (publicArticle == null) { 
			throw new IllegalArgumentException("publicarticle"); 
		} 
		repo.update(publicArticle); 
	} 
 
 	/**@author TuanTM20
  	 * 27/02/2014 - 11:34:02
  	 * For: 
  	 * @description: 
  	 */
 	@Override 
	public void deletePublicArticle(PublicArticle publicArticle) throws DataAccessException { 
		if (publicArticle == null) { 
			throw new IllegalArgumentException("publicarticle"); 
		} 
		repo.delete(publicArticle); 
	}

	@Override
	public List<PublicArticleSearchVO> getListPublicArticleByFilter(
			PublicArticleSearchFilter filter) throws DataAccessException {
		List<Object> paramSelect = new ArrayList<Object>();
		List<Object> countParams = new ArrayList<Object>();
		StringBuilder selectSql = new StringBuilder();
		StringBuilder countQuery = new StringBuilder();
		StringBuilder fromQuery = new StringBuilder();
		
		selectSql.append(" SELECT PA.id 	as id, ");
		selectSql.append(" PA.name 			as name, ");
		selectSql.append(" PA.avatar_url 	as avatarUrl, ");
		selectSql.append(" PA.avatar_thumb 	as avatarThumb, ");
		selectSql.append(" PA.content 		as content, ");
		selectSql.append(" PA.content_html 	as contentHtml, ");
		selectSql.append(" PA.short_content	as shortContent, ");
		selectSql.append(" AG.id 			as articleCategoryId, ");
		selectSql.append(" AG.name 			as articleCategoryName, ");
		selectSql.append(" PA.status 		as status, ");
		selectSql.append(" US.id 			as cmsUserId, ");
		selectSql.append(" US.username 		as createdUser, ");
		selectSql.append(" PA.created_time 	as createdTime ");
		
		fromQuery.append(" FROM Public_Article PA ");
		fromQuery.append(" 		LEFT JOIN ARTICLE_CATEGORY AG ");
		fromQuery.append(" 			ON PA.article_category_id = AG.id ");
		fromQuery.append(" 		LEFT JOIN CMS_USER US ");
		fromQuery.append(" 			ON PA.cms_user_id = US.id ");

		fromQuery.append(" where PA.status != ? ");
		paramSelect.add(ArticleStatus.DELETED.getValue());
		countParams.add(ArticleStatus.DELETED.getValue());
		
		if (filter.getId() != null) {
			fromQuery.append(" and PA.id = ? ");
			paramSelect.add(filter.getId());
			countParams.add(filter.getId());
		}
		
		if (!StringUtil.isNullOrEmpty(filter.getNameText())) {
			fromQuery.append("AND lower(PA.name_text) LIKE lower(?) escape '/' ");
			paramSelect.add("%" + StringUtil.toSearchText(filter.getNameText()) + "%");
			countParams.add("%" + StringUtil.toSearchText(filter.getNameText()) + "%");
		}
		
		if (!StringUtil.isNullOrEmpty(filter.getContentText())) {
			fromQuery.append("AND lower(PA.content_text) LIKE lower(?) escape '/' ");
			paramSelect.add("%" + StringUtil.toSearchText(filter.getContentText()) + "%");
			countParams.add("%" + StringUtil.toSearchText(filter.getContentText()) + "%");
		}
		
		if (filter.getArticleCategoryId()!= null) {
			fromQuery.append(" and PA.article_category_id = ? ");
			paramSelect.add(filter.getArticleCategoryId());
			countParams.add(filter.getArticleCategoryId());
		}
		
		if (filter.getStatus() != null) {
			fromQuery.append(" and PA.status = ? ");
			paramSelect.add(filter.getStatus());
			countParams.add(filter.getStatus());
		}
		
		if(filter.getFromDate()!=null && filter.getToDate()!=null ){
			fromQuery.append("  AND TRUNC(PA.CREATED_TIME) BETWEEN TRUNC(?) AND TRUNC(?) ");
			paramSelect.add(filter.getFromDate());
			countParams.add(filter.getFromDate());
			
			paramSelect.add(filter.getToDate());
			countParams.add(filter.getToDate());
		}
		
		selectSql.append(fromQuery).append(" ORDER BY PA.created_time DESC");

		countQuery.append("SELECT COUNT(*) AS COUNT ").append(fromQuery);

		String[] fieldNames = new String[] { 
				"id", "name", 
				"avatarUrl", "avatarThumb", 
				"content",  "contentHtml",  "shortContent", 
				"articleCategoryId","articleCategoryName",
				"status", "cmsUserId",
				"createdUser","createdTime"
				};
		Type[] fieldTypes = new Type[] { 
				StandardBasicTypes.INTEGER,StandardBasicTypes.STRING, 
				StandardBasicTypes.STRING, StandardBasicTypes.STRING, 
				StandardBasicTypes.STRING, StandardBasicTypes.STRING, StandardBasicTypes.STRING,
				StandardBasicTypes.INTEGER,StandardBasicTypes.STRING, 
				StandardBasicTypes.INTEGER,StandardBasicTypes.INTEGER, 
				StandardBasicTypes.STRING, StandardBasicTypes.DATE };

		List<Class<?>> synchronizedClass = new ArrayList<Class<?>>();
		synchronizedClass.add(PublicArticle.class);
		synchronizedClass.add(ArticleCategory.class);
		synchronizedClass.add(CmsUser.class);

		List<PublicArticleSearchVO> list = null;
		if (filter.getPaging() != null) {
			list = repo.getListByQueryAndScalarPaginated(
					PublicArticleSearchVO.class, fieldNames, fieldTypes,
					selectSql.toString(), countQuery.toString(), paramSelect,
					countParams, filter.getPaging(), synchronizedClass);
		} else {
			list = repo.getListByQueryAndScalar(PublicArticleSearchVO.class,
					fieldNames, fieldTypes, selectSql.toString(), paramSelect,
					synchronizedClass);
		}

		return list;
	}

} 
 
